package com.nengchuan.eic.nceconomy.controller;


import com.github.pagehelper.PageInfo;
import com.nengchuan.eic.nceconomy.entity.dto.SwDirectoryPageDTO;
import com.nengchuan.eic.nceconomy.entity.dto.SwDirectorySaveDTO;
import com.nengchuan.eic.nceconomy.entity.po.SwDirectory;
import com.nengchuan.eic.nceconomy.entity.vo.SwDirectoryVO;
import com.nengchuan.eic.nceconomy.service.SwDirectoryService;
import com.nengchuan.eic.nceconomy.util.HttpUtils;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.MapperUtils;
import com.nsc.boot.sdk.common.utils.AuthUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 三维测算目录相关controller
 * </p>
 *
 * @author panwen
 * @since 2020-05-25
 */
@Api(value = "三维测算目录",tags = "三维测算目录")
@RestController
@RequestMapping("/sw_directory")
@Slf4j
@Validated
public class SwDirectoryController {

    @Autowired
    private SwDirectoryService swDirectoryService;

    @ApiOperation("目录分页列表(分页+条件)")
    @GetMapping("/page")
    public WrappedResult<PageInfo<SwDirectoryVO>> page(@Valid SwDirectoryPageDTO dto){
        return WrappedResult.success(swDirectoryService.page(dto));
    }

    @ApiOperation("新增或更新")
    @PostMapping("/save_or_update")
    public WrappedResult<Boolean> saveOrUpdate(@Valid SwDirectorySaveDTO dto){
        SwDirectory swDirectory= MapperUtils.mapping(dto,SwDirectory.class);
        boolean status=swDirectoryService.saveOrUpdate(swDirectory);
        long userId= AuthUtil.getUserId();
        log.info("#saveOrUpdate# 正在进行三维测算目录{}; [swDirId:{},userId:{},status:{}]", HttpUtils.getOperType(dto.getId()),dto.getId(),userId,status);
        return WrappedResult.success(status);
    }

    @ApiOperation("删除")
    @PostMapping("/remove/{id}")
    public WrappedResult<Boolean> remove(@PathVariable("id") @NotNull(message = "id不能为空") @Min(value = 1,message = "id不符合格式") Long id){
        boolean status=swDirectoryService.removeById(id);
        long userId= AuthUtil.getUserId();
        log.info("#saveOrUpdate# 正在进行三维测算目录DELETE; [swDirId:{},userId:{},status:{}]",id,userId,status);
        return WrappedResult.success(status);
    }
}
